package com.lagou.config;

import com.lagou.constant.RabbitMQConstant;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: qjc
 * @Date: 2021/12/9
 */
@Configuration
public class QueueConfig {

    @Bean(name = RabbitMQConstant.QUEUE_DLX_NORMAL)
    public Queue dlxNormalQueue() {
        Map<String, Object> arguments = new HashMap<>(4);
        // 设置队列中所有消息过期时间30s
        arguments.put("x-message-ttl", 30000);
        // 设置该队列所关联的死信交换器（当队列消息TTL到期后依然没有被消费，则加入死信队列）
        arguments.put("x-dead-letter-exchange", RabbitMQConstant.EXCHANGE_DLX);
        // 设置该队列所关联的死信交换器的routingkey，如果没有特殊指定，使用原来队列的routingkey
        arguments.put("x-dead-letter-routing-key", RabbitMQConstant.ROUTING_KEY_DLX);

        return new Queue(RabbitMQConstant.QUEUE_DLX_NORMAL, true, false, false, arguments);
    }

    @Bean(name = RabbitMQConstant.QUEUE_DLX)
    public Queue dlxQueue() {
        return new Queue(RabbitMQConstant.QUEUE_DLX, true, false, false);
    }

}
